1 00:00:00,810 --> 00:00:04,830 Today I'm going to introduce a programming construct called a Table. 2 00:00:04,830 --> 00:00:09,750 So when we want to store a bunch of things under one variable, we use a table. 3 00:00:09,780 --> 00:00:13,680 Tables hold a collection of things, whether they're numbers or parts. 4 00:00:13,680 --> 00:00:17,130 Zombies, players, you can see how this is going to be very handy. 5 00:00:17,550 --> 00:00:21,390 This is a fresh world right here, only to only to show you tables. 6 00:00:21,540 --> 00:00:28,800 I'm going to go to workspace, hit the plus, add a script, and this script I'm going to call table 7 00:00:28,800 --> 00:00:29,760 intro. 8 00:00:32,030 --> 00:00:33,050 Cool beans. 9 00:00:33,200 --> 00:00:34,460 We'll get rid of that print statement. 10 00:00:34,460 --> 00:00:35,390 We don't need it. 11 00:00:35,630 --> 00:00:40,280 And when I do tables, there are two basic types of tables. 12 00:00:40,280 --> 00:00:46,040 We've got a list type of table, and we've got a key value pair of table, both tables, slightly different 13 00:00:46,040 --> 00:00:48,170 behaviour depending on what you have in the table. 14 00:00:49,100 --> 00:00:58,520 I'll put a comment here, put those two dashes, list type table and then I'll make a variable local. 15 00:00:58,520 --> 00:01:03,680 My list table equals will initialize it with some data. 16 00:01:05,150 --> 00:01:07,970 Use these curly brackets because it's a table. 17 00:01:07,970 --> 00:01:09,380 That's how you initialize a table. 18 00:01:10,430 --> 00:01:11,540 Let's put strings in there. 19 00:01:11,540 --> 00:01:13,340 Let's put strings of monster names. 20 00:01:13,340 --> 00:01:24,230 So I'm going to say zombie and I'll say Ghost and Goblin. 21 00:01:24,800 --> 00:01:25,700 Is that one B? 22 00:01:25,700 --> 00:01:26,630 I think so. 23 00:01:27,140 --> 00:01:27,440 All right. 24 00:01:27,440 --> 00:01:29,030 So there's three elements in here. 25 00:01:29,030 --> 00:01:37,790 One, two, three, three things in my table under one variable, and let's get the other the other 26 00:01:37,790 --> 00:01:42,500 type of table data or the other type of table key value pair data. 27 00:01:42,590 --> 00:01:42,870 Right. 28 00:01:42,920 --> 00:01:48,740 So I'll say a key value pair of table local, my cave table. 29 00:01:48,770 --> 00:01:50,030 I thought that's a good name. 30 00:01:50,030 --> 00:01:55,670 We need curly brackets again because it's still a table and now we're going to come up with key value 31 00:01:55,670 --> 00:01:56,210 pairs. 32 00:01:56,210 --> 00:02:02,000 So what I want to store in here, let's say like player data. 33 00:02:02,000 --> 00:02:06,680 So we'll do like name equals Ralph. 34 00:02:06,960 --> 00:02:07,560 All right. 35 00:02:09,880 --> 00:02:10,840 So this is a string. 36 00:02:10,840 --> 00:02:12,590 So we need we need speech quotes. 37 00:02:12,610 --> 00:02:14,640 Next one I think I'm going to do is XP. 38 00:02:14,650 --> 00:02:15,430 That's a number. 39 00:02:15,430 --> 00:02:17,400 So we don't need speech quotes. 40 00:02:17,410 --> 00:02:21,100 And then the third one, how about title? 41 00:02:22,000 --> 00:02:23,080 What's his title? 42 00:02:23,200 --> 00:02:24,460 Noob. 43 00:02:24,670 --> 00:02:24,990 Right. 44 00:02:25,030 --> 00:02:26,770 3500 seems pretty low. 45 00:02:27,190 --> 00:02:31,070 All right, let's print out the data in the output window to see what we have in there. 46 00:02:31,150 --> 00:02:34,780 So, say, print my list data. 47 00:02:34,930 --> 00:02:36,250 My list table. 48 00:02:36,640 --> 00:02:39,460 Print my cave table. 49 00:02:40,000 --> 00:02:40,880 Cool beans. 50 00:02:40,900 --> 00:02:42,430 Let's go to our view. 51 00:02:42,460 --> 00:02:45,700 Let's open up our output window right here. 52 00:02:46,420 --> 00:02:48,930 With this new annoying icon. 53 00:02:48,940 --> 00:02:49,900 Now, I figure. 54 00:02:50,050 --> 00:02:51,130 Finally figure it out. 55 00:02:51,250 --> 00:02:51,840 All right. 56 00:02:51,850 --> 00:02:56,680 Now, if you have default settings set for your output window, you're not going to see the data in 57 00:02:56,680 --> 00:02:58,480 your tables when you print them out. 58 00:02:58,780 --> 00:03:01,180 We can move this down a little bit for these. 59 00:03:01,270 --> 00:03:03,820 What we need to do is go to these three dots. 60 00:03:03,940 --> 00:03:07,960 Make sure log mode is off default. 61 00:03:07,960 --> 00:03:09,000 It's going to be on. 62 00:03:09,010 --> 00:03:10,700 You're only going to see the table address. 63 00:03:10,720 --> 00:03:13,000 It's not going to be useful off. 64 00:03:13,300 --> 00:03:13,910 Cool. 65 00:03:13,930 --> 00:03:14,740 Let's run it. 66 00:03:17,230 --> 00:03:18,010 There we go. 67 00:03:18,010 --> 00:03:19,470 So long mode off. 68 00:03:19,480 --> 00:03:21,670 You're going to see these brackets with these three dots. 69 00:03:21,670 --> 00:03:23,430 You can open that up. 70 00:03:23,440 --> 00:03:24,340 There you go. 71 00:03:24,340 --> 00:03:27,060 There's your data and it's got numbers, right? 72 00:03:27,070 --> 00:03:28,960 One, two, three. 73 00:03:28,990 --> 00:03:30,010 These are an order. 74 00:03:30,010 --> 00:03:32,410 Zombie ghost goblin. 75 00:03:32,440 --> 00:03:34,000 Let's take a look down here. 76 00:03:36,200 --> 00:03:36,530 Oh. 77 00:03:36,530 --> 00:03:37,340 What happened here? 78 00:03:37,340 --> 00:03:38,290 We got our name. 79 00:03:38,300 --> 00:03:39,080 That's good. 80 00:03:39,260 --> 00:03:42,200 Title and XP switched. 81 00:03:42,320 --> 00:03:42,950 Right. 82 00:03:43,280 --> 00:03:45,950 So it goes to show you that. 83 00:03:45,950 --> 00:03:47,000 And you got to remember this. 84 00:03:47,000 --> 00:03:51,710 The table data with key value pairs order isn't guaranteed. 85 00:03:51,710 --> 00:03:54,200 You'd have to do something to enforce order here. 86 00:03:54,200 --> 00:03:55,760 This is Alexa graphical order. 87 00:03:55,760 --> 00:03:57,170 So it's going by alphabet. 88 00:03:57,170 --> 00:04:03,020 But if I made exp capital it would be up here because capital letters start before lowercase letters 89 00:04:03,020 --> 00:04:04,490 and Alexa graphical ordering. 90 00:04:04,670 --> 00:04:06,140 Anyway, you don't need to know that. 91 00:04:06,140 --> 00:04:08,000 Just know that order is not guaranteed. 92 00:04:08,510 --> 00:04:09,110 Cool. 93 00:04:09,110 --> 00:04:10,280 Let's continue. 94 00:04:11,290 --> 00:04:11,590 All right. 95 00:04:11,590 --> 00:04:13,930 Let's go ahead and close this so we have some more room. 96 00:04:14,140 --> 00:04:21,460 I want to get a particular element in each table and we'll just print it out. 97 00:04:21,460 --> 00:04:23,140 We won't put it in variables or anything. 98 00:04:23,680 --> 00:04:27,640 So print and I won't say list type. 99 00:04:28,840 --> 00:04:30,130 Tables. 100 00:04:30,830 --> 00:04:37,650 Second element equals, well, let's get our list type table. 101 00:04:37,670 --> 00:04:41,060 Now you're going to need these square brackets. 102 00:04:41,390 --> 00:04:44,030 These are square brackets when you're getting an element. 103 00:04:44,060 --> 00:04:46,490 These are curly brackets when you're declaring. 104 00:04:46,850 --> 00:04:47,950 That's a little tricky. 105 00:04:47,960 --> 00:04:50,230 So we want to get let's get ghost. 106 00:04:50,240 --> 00:04:51,860 That's the second element. 107 00:04:51,980 --> 00:04:56,330 And Lua, we start 1 to 3 in like C and Java. 108 00:04:56,360 --> 00:04:57,170 You start with zero. 109 00:04:57,320 --> 00:04:58,400 Here we start with one. 110 00:04:58,400 --> 00:05:04,970 So if we want to ghost two, let's print out one for the key value pair. 111 00:05:05,180 --> 00:05:12,140 Print k v type tables. 112 00:05:12,590 --> 00:05:13,800 Name key. 113 00:05:13,820 --> 00:05:15,410 We'll type out the name. 114 00:05:17,210 --> 00:05:18,380 How do you do that? 115 00:05:19,250 --> 00:05:21,200 I think it's a little more intuitive. 116 00:05:21,230 --> 00:05:23,870 Let's get my key value table. 117 00:05:24,440 --> 00:05:28,170 Dot all of that even gives you even tells you what's available. 118 00:05:28,190 --> 00:05:29,150 I want a name. 119 00:05:29,630 --> 00:05:30,440 There you go. 120 00:05:30,980 --> 00:05:31,900 Let's print that out. 121 00:05:31,910 --> 00:05:32,630 Go to view. 122 00:05:32,660 --> 00:05:33,740 Go to output. 123 00:05:34,430 --> 00:05:35,240 Hit the print. 124 00:05:39,700 --> 00:05:40,690 Let's take a look. 125 00:05:40,720 --> 00:05:41,470 Let's type. 126 00:05:41,500 --> 00:05:41,800 Table. 127 00:05:41,800 --> 00:05:42,580 Second element. 128 00:05:42,580 --> 00:05:43,280 Ghost. 129 00:05:43,300 --> 00:05:43,900 Nice. 130 00:05:43,900 --> 00:05:44,770 Is it Ghost? 131 00:05:44,800 --> 00:05:45,970 It is ghost. 132 00:05:46,870 --> 00:05:52,570 And then here k v type table's name. 133 00:05:52,600 --> 00:05:55,360 That should be a CVV type table's name. 134 00:05:55,780 --> 00:05:56,620 Ralph. 135 00:05:58,960 --> 00:06:02,140 Let's go ahead and fix my typo so that we're not confused. 136 00:06:02,380 --> 00:06:03,760 Key value, right? 137 00:06:03,790 --> 00:06:04,300 That's what you want. 138 00:06:04,330 --> 00:06:05,620 Key value. 139 00:06:06,870 --> 00:06:07,210 All right. 140 00:06:07,230 --> 00:06:09,540 How do we insert an element? 141 00:06:10,850 --> 00:06:14,260 Well, it's not that bad in the list type. 142 00:06:14,270 --> 00:06:15,580 There's many ways to do it. 143 00:06:15,590 --> 00:06:16,820 I'm just going to pick one. 144 00:06:16,820 --> 00:06:17,630 Just pick one. 145 00:06:17,630 --> 00:06:18,170 Right. 146 00:06:18,170 --> 00:06:19,280 This is one of them. 147 00:06:19,280 --> 00:06:20,660 Table insert. 148 00:06:20,660 --> 00:06:20,990 Right. 149 00:06:20,990 --> 00:06:23,660 So this is provided to you by Roblox. 150 00:06:23,660 --> 00:06:32,690 It's a it's a Lua function actually and you could do your table name or this is how you do it and then 151 00:06:32,690 --> 00:06:34,370 put what you want in there. 152 00:06:34,430 --> 00:06:36,170 I'm going to say werewolf. 153 00:06:39,260 --> 00:06:42,560 And that's going to append it at the end of the list. 154 00:06:42,560 --> 00:06:42,980 Right. 155 00:06:42,980 --> 00:06:44,250 So it's going to be here. 156 00:06:44,270 --> 00:06:49,880 Now, you can actually change where you want it to be placed in the list, but we'll do that in other 157 00:06:49,880 --> 00:06:50,420 videos. 158 00:06:50,420 --> 00:06:51,860 I want to keep this basic. 159 00:06:51,860 --> 00:06:57,020 How do you add let's say we want to add level to this table. 160 00:06:58,310 --> 00:07:03,440 Keep in mind, these tables are initialized, doesn't have to have data in them, but they do have to 161 00:07:03,440 --> 00:07:04,310 be initialized. 162 00:07:04,310 --> 00:07:08,840 So if you initialize it with no data like that, it will be fine. 163 00:07:08,840 --> 00:07:11,240 But if you have a nil here, you're going to run into trouble. 164 00:07:11,680 --> 00:07:11,940 All right. 165 00:07:11,960 --> 00:07:14,150 I just did a control Z to put that back. 166 00:07:14,540 --> 00:07:22,310 Let's go and get our let's go ahead and put an insert of value a level the level key in my key value 167 00:07:22,310 --> 00:07:22,880 table. 168 00:07:23,580 --> 00:07:26,900 Dot, look at this level. 169 00:07:27,320 --> 00:07:29,120 Nice too. 170 00:07:29,120 --> 00:07:31,330 And now we want to be able to see our data. 171 00:07:31,340 --> 00:07:33,530 We could get these prints and go down here. 172 00:07:33,530 --> 00:07:36,200 But I also want to show you how to do loops. 173 00:07:36,200 --> 00:07:36,680 Right. 174 00:07:36,680 --> 00:07:42,470 And in in tables we use a key value pair loop. 175 00:07:42,590 --> 00:07:43,400 What's it called? 176 00:07:43,400 --> 00:07:47,420 It is called a for loop in pairs. 177 00:07:47,420 --> 00:07:56,630 Whether you're doing the list type or the key value type, watch for I and V in pairs. 178 00:07:56,630 --> 00:07:58,580 Put your table name in here. 179 00:07:58,580 --> 00:08:00,320 We'll do the list type first. 180 00:08:01,010 --> 00:08:07,550 Do so this if it's a list type table, this is going to be one, two, three, four. 181 00:08:07,550 --> 00:08:08,750 It's going to be the numbers. 182 00:08:08,750 --> 00:08:10,700 This is going to be the values. 183 00:08:10,730 --> 00:08:13,670 Zombie ghost goblin werewolf. 184 00:08:13,670 --> 00:08:13,950 Right. 185 00:08:14,060 --> 00:08:15,050 And operate them out. 186 00:08:15,530 --> 00:08:16,490 Let's do a print statement. 187 00:08:17,390 --> 00:08:23,000 Print I equals I, v equals v. 188 00:08:23,900 --> 00:08:26,390 And this is how convenient this for loop is. 189 00:08:26,390 --> 00:08:27,500 Let's go ahead and copy it. 190 00:08:27,500 --> 00:08:34,040 Control C, control V and then let's put our other table in here. 191 00:08:34,870 --> 00:08:35,800 Just paste it. 192 00:08:36,010 --> 00:08:37,390 My cave table. 193 00:08:37,390 --> 00:08:38,820 What's I going to be? 194 00:08:38,830 --> 00:08:39,880 It's going to be the keys. 195 00:08:39,880 --> 00:08:40,840 It's going to be name. 196 00:08:40,840 --> 00:08:41,980 It's going to be XP. 197 00:08:41,980 --> 00:08:43,090 It's going to be level. 198 00:08:43,090 --> 00:08:44,740 What are the V's going to be? 199 00:08:44,740 --> 00:08:48,640 It's going to be Ralph 3600 or whatever. 200 00:08:48,640 --> 00:08:50,170 That number was 3500. 201 00:08:50,170 --> 00:08:52,300 Nube And then we added level two. 202 00:08:52,300 --> 00:08:54,790 So it's going to print out, let's try it out. 203 00:08:59,020 --> 00:08:59,860 Let's take a look. 204 00:08:59,890 --> 00:09:01,930 View output. 205 00:09:02,230 --> 00:09:03,760 Check it out. 206 00:09:04,390 --> 00:09:05,830 So we have our numbers. 207 00:09:05,830 --> 00:09:07,180 One, two, three, four. 208 00:09:07,180 --> 00:09:09,790 Werewolf was, in fact, appended to the bottom. 209 00:09:10,590 --> 00:09:11,120 Title. 210 00:09:11,130 --> 00:09:12,070 Oh, look at this. 211 00:09:12,090 --> 00:09:14,490 So now it's not even lexical order. 212 00:09:14,490 --> 00:09:20,100 We have title, we have level, which should be first if we're doing some sort of ordering name and 213 00:09:20,100 --> 00:09:20,640 exp. 214 00:09:20,670 --> 00:09:27,240 Remember, key value pairs order is not guaranteed, but we have all of our elements and we printed 215 00:09:27,240 --> 00:09:29,280 through them using the for loop. 216 00:09:29,310 --> 00:09:31,350 Ooh, I got to share one more thing. 217 00:09:31,350 --> 00:09:34,950 I got a comment on this on one of my YouTube videos. 218 00:09:35,460 --> 00:09:46,680 If you are using a list type of of table, it has numbers one, two, three, four use IE pairs. 219 00:09:46,710 --> 00:09:48,630 That's a little more efficient, right? 220 00:09:48,630 --> 00:09:50,280 That's for integer pairs. 221 00:09:50,280 --> 00:09:52,080 You're going to get the exact same thing. 222 00:09:52,080 --> 00:09:53,580 What's going to be crazy? 223 00:09:53,580 --> 00:09:53,940 You know what? 224 00:09:53,940 --> 00:09:57,780 I'm not going to go into it because it's an intro, introduction to tables. 225 00:09:57,780 --> 00:09:58,650 Let's print it out. 226 00:09:58,650 --> 00:09:59,670 Let's just see what we got. 227 00:10:01,840 --> 00:10:06,400 Look at that, exactly the same use in pairs or use pairs. 228 00:10:06,430 --> 00:10:10,900 I don't think the efficiency is going to change much on the level that we're doing. 229 00:10:11,110 --> 00:10:13,930 We're not we're not doing megabytes of data. 230 00:10:13,960 --> 00:10:20,740 So with that, I will see you in the next section when we start building our games and we're going to 231 00:10:20,740 --> 00:10:23,140 do a lot more stuff with tables.